#!/bin/sh
########################################################################
# Begin postgresql
#
# Description : Start PostgreSQL Server
#
# Author      : Ken Moffat - ken@linuxfromscratch.org
#
# Version     : BLFS 7.0
#
# Notes       : Based on the earlier version by Gerard Beekmans
#
########################################################################

### BEGIN INIT INFO
# Provides:            postgresql
# Required-Start:      $network
# Should-Start:        networkmanager wicd
# Required-Stop:       $network
# Should-Stop:         networkmanager wicd
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts PostgreSQL server.
# Description:         Starts PostgreSQL server.
# X-LFS-Provided-By:   BLFS
### END INIT INFO

. /lib/lsb/init-functions

#$LastChangedBy: pierre $
#$Date: 2020-10-02 00:59:59 -0500 (Fri, 02 Oct 2020) $

case "$1" in
   start)
      log_info_msg "Starting PostgreSQL daemon..."

      install -dm755 -o postgres -g postgres /run/postgresql

      su - postgres -c '/usr/bin/pg_ctl start -s -D /srv/pgsql/data \
                         -l /srv/pgsql/data/logfile -o "-i" '
      evaluate_retval
      ;;

   stop)
      log_info_msg "Stopping PostgreSQL daemon..."
      su - postgres -c "/usr/bin/pg_ctl stop -m smart -s -D /srv/pgsql/data"
      evaluate_retval
      ;;

   restart)
      $0 stop
      sleep 1
      $0 start
      ;;

   status)
      su - postgres -c "/usr/bin/pg_ctl status -D /srv/pgsql/data"
      ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac

# End /etc/init.d/postgresql
